/**
 *
 * 问答控制器
 * 
 */
class QuestionController extends Controller {
    private questionViews: Array<QuestionView>;

    private questionResLoaded: Array<boolean>;
    
    private questionNum:number = 6;

    public constructor() {
        super();
    }
    /**
    * 初始化控制器,
    */
    protected init(): void {
        super.init();
        this.questionViews = new Array<QuestionView>();
        this.questionResLoaded = new Array<boolean>();
    }

    /**
     * 监听事件
     */
    protected initListeners(): void {
        super.initListeners();
        this.addCommand(CMDEvent.OPEN_QUESTION,this.showQuestion,this);
        this.addCommand(CMDEvent.LOAD_QUESTION_RES,this.resLoadComplete,this);
    }
    /**
     * 展示问题
     */
    private showQuestion(event: egret.Event): void {
        /** 1-6 表示问题1-6 已经加载完毕*/
        var index: number = event.data;
        Log.d("showQuestion,index:" + index);
        //资源加载完毕，展示问题页
        if(this.questionResLoaded[index]) {
            if(this.questionViews[0] != null) {
                this.root.removeChildren();
            }
            if(this.questionViews[index] == null) {
                this.questionViews[index] = new QuestionView(index);
                this.questionViews[index].addEventListener(CMDEvent.CHOOSE_ANSWER,this.chooseAnswer,this)
            }else{
                Log.d("view has cache:" + index);
            }
            
            this.root.addChildAt(this.questionViews[index],0);
            if(index >= this.questionNum-1) {
                this.dispatch(CMDEvent.LOAD_LOADING,CMDEvent.GROUP_RESULT);
            } else {
                this.dispatch(CMDEvent.LOAD_LOADING,this.indexToResName(++index));
            } 
        } else {
            //展示Loading
            this.dispatch(CMDEvent.SHOW_LOADING);
        }
    }
    
    /**
     * 资源加载完毕
     */
    private resLoadComplete(event: egret.Event): void {
        var groupName: string = event.data;
        if(groupName != null) {
            var index: number = this.resNameToIndex(groupName);
            this.questionResLoaded[index] = true;
            var num = this.root.numElements;
            if(num >= 2 && this.root.getChildAt(num - 1) instanceof LoadingView) {
                this.dispatch(CMDEvent.OPEN_QUESTION,index);
            }
        }
    }
    
    /**
     * 选择答案
     */
    private chooseAnswer(event: egret.Event): void {
        var index: number = event.data;
        Log.d("chooseAnswer:" + index);
        if(index < this.questionNum){
            //打开下一页
            this.dispatch(CMDEvent.OPEN_QUESTION,index);
        }else{
            //打开结果页
            this.dispatch(CMDEvent.OPEN_RESUlT,index);
        }
    }


    private indexToResName(index: number): string {
        var resName: string = null;
        switch(index) {
            case 0:
                resName = CMDEvent.GROUP_QUESTION_1;
                break;
            case 1:
                resName = CMDEvent.GROUP_QUESTION_2;
                break;
            case 2:
                resName = CMDEvent.GROUP_QUESTION_3;
                break;
            case 3:
                resName = CMDEvent.GROUP_QUESTION_4;
                break;
            case 4:
                resName = CMDEvent.GROUP_QUESTION_5;
                break;
            case 5:
                resName = CMDEvent.GROUP_QUESTION_6;
                break;
        }
        return resName;
    }

    private resNameToIndex(resName: string): number {
        var index: number = 0;
        if(resName == CMDEvent.GROUP_QUESTION_1) {
            index = 0;
        } else if(resName == CMDEvent.GROUP_QUESTION_2) {
            index = 1;
        } else if(resName == CMDEvent.GROUP_QUESTION_3) {
            index = 2;
        } else if(resName == CMDEvent.GROUP_QUESTION_4) {
            index = 3;
        } else if(resName == CMDEvent.GROUP_QUESTION_5) {
            index = 4;
        } else if(resName == CMDEvent.GROUP_QUESTION_6) {
            index = 5;
        }
        return index;
    }


}
